package top.lishuoboy.boot91.sql.init.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class Boot91Controller {

    @Autowired
    DataSourceScriptDatabaseInitializer databaseInitializer;
    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("/initDB")
    private void initDB() {
        jdbcTemplate.execute("insert into SYS_USER (id, name) values('3', '王五')");
        log.warn("userList=={}", jdbcTemplate.queryForList("select * from SYS_USER"));      // userList==[{ID=1, NAME=张三}, {ID=2, NAME=李四}, {ID=3, NAME=王五}]
        databaseInitializer.initializeDatabase();
        log.warn("userList=={}", jdbcTemplate.queryForList("select * from SYS_USER"));      // userList==[{ID=1, NAME=张三}, {ID=2, NAME=李四}]
    }
}
